package net.liteheaven.xblib.common.convert;

import android.text.Html;
import android.text.Spanned;

import java.util.List;

/**
 * Created by LiangYu on 2017/2/13
 */
public class FormatUtil {

    public static int string2Int(String val, int def){
        int result;
        try{
            result = Integer.valueOf(val);
        }catch(Exception e){
            result = def;
        }
        return result;
    }

    public static long string2Long(String val, long def){
        long result;
        try{
            result = Integer.valueOf(val);
        }catch(Exception e){
            result = def;
        }
        return result;
    }

    public static class ColorTextInfo{
        public String color;
        public String text;
        public ColorTextInfo(String color, String text){
            this.color = color;
            this.text = text;
        }
    }

    /**
     * 获取颜色字符串
     * @param infos
     * @return
     */
    public static Spanned getColorText(List<ColorTextInfo> infos){
        String str = "";
        for (ColorTextInfo info : infos) {
            str += "<font color=\"#" + info.color + "\">" + info.text + "</font>";
        }
        Spanned result = Html.fromHtml(str);
        return result;
    }

}

